/**
 * 
 */
package com.luck.pf.framework.action;

import java.util.List;

import com.luck.pf.common.entity.TreeVo;
import com.luck.pf.exception.BusinessException;


/**
 * 树形机构扩展
 *
 */
public class BaseTreeAction extends BaseActionSupport {
	private static final long serialVersionUID = 3181456784687071295L;
	/**
	 * 根节点ID
	 */
	protected Long rootId;
	/**
	 * 根节点名称
	 */
	protected String rootTreeName;
	/**
	 * 树形结构
	 */
	protected List treeList;
	
	/**
	 * 是否拥有子节点
	 * @param menuList
	 * @param menuId
	 * @return
	 */
	protected boolean hasChild(List treeList,Long treeId){
		boolean result = true;
		TreeVo next = null;
		for (int i = 0, len = treeList.size(); i < len; i++){
			next = (TreeVo)treeList.get(i);
			if(next.getParentId()!=null&&treeId.longValue()==next.getParentId().longValue()){
				result=false;
				break;
			}
		}
		return result;
	}
	
	/**
	 * 
	 * @param treeList
	 */
	protected void nodeIsLeaf(List treeList){
		TreeVo next = null;
		for (int i = 0, len = treeList.size(); i < len; i++){
			next = (TreeVo)treeList.get(i);
			next.setLeaf(hasChild(treeList, next.getTreeId()));
		}
	}
	
	/**
	 * 判断是否是叶子节点
	 * @param treeList
	 * @param treeId
	 * @return
	 */
	protected boolean nodeHasChild(List treeList,Long treeId){
		boolean result = true;
		TreeVo next = null;
		for (int i = 0, len = treeList.size(); i < len; i++){
			next = (TreeVo)treeList.get(i);
			if(treeId.longValue()==next.getParentId().longValue()){
				result=false;
				break;
			}
		}
		return result;
	}
	/**
	 * 构建树形结构
	 * @param treeList
	 * @param treeId
	 * @param rootTreeId
	 * @param xml
	 * @param loops
	 * @return
	 */
	protected int makeTreeXml(List treeList,Long treeId,Long rootTreeId,StringBuffer xml,int loops)throws BusinessException{
		loops++;
		if(loops>20000){
			throw new BusinessException("菜单数据存在死循环，请调整菜单数据");
		}
		TreeVo next = null;
		for (int i = 0, len = treeList.size(); i < len; i++) {
			next = (TreeVo)treeList.get(i);
			if(next.getTreeId().longValue()==rootTreeId.longValue()){
				continue;
			}
			if(next.getParentId().longValue()==treeId.longValue()){
				xml.append("<li class=\"jstree-closed\" id=\"id_");
				xml.append(next.getTreeId());
				xml.append("\">");
				xml.append("<a href=\"###\">");
				xml.append(next.getTreeName());
				xml.append("</a>");
				if(!next.isLeaf()){
					xml.append("<ul>\r\n");
				}
				loops = this.makeTreeXml(treeList, next.getTreeId(), rootTreeId, xml, loops);
				if(!next.isLeaf()){
					xml.append("</ul>\r\n");
				}
				xml.append("</li>\r\n");
			}
		}
		return loops;
	}
	
	/**
	 * 树状结构
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String getXmlTree() throws Exception {
		//HttpServletRequest request = super.getRequest();
		//HttpServletResponse response = super.getResponse();
		nodeIsLeaf(treeList);
		TreeVo root = (TreeVo)treeList.get(0);
		this.rootId = root.getTreeId();
		this.rootTreeName = root.getTreeName();
		StringBuffer treeXml = new StringBuffer("");
		treeXml.append("<ul>");
		treeXml.append("<li id=\"id_");
		treeXml.append(this.rootId);
		treeXml.append("\" class=\"jstree-open\"><a href=\"###\">");
		treeXml.append(this.rootTreeName);
		treeXml.append("</a><ul>");
		makeTreeXml(treeList, this.rootId, this.rootId, treeXml, 1);
		treeXml.append("</ul></li>");
		treeXml.append("</ul>");		
		return treeXml.toString();
	}
	
}
